package com.jaryle.wenxin.util.message;

import com.jaryle.wenxin.bean.base.Image;
import com.jaryle.wenxin.bean.message.ImageMessage;
import com.jaryle.wenxin.util.UploadUtil;
import com.jaryle.wenxin.util.WeiXinUtil;
import com.thoughtworks.xstream.XStream;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.util.Date;

/**
 * 描述:
 * 图片消息工具类
 *
 * @author jaryle
 * @version V1.0
 * @create 2018-12-24
 */
public class ImageMessageUtil implements  BaseMessageUtil<ImageMessage>{
    @Override
    public String messageToXml(ImageMessage message) {
        XStream xtream = new XStream();
        xtream.alias("xml", message.getClass());
        xtream.alias("Image", new Image().getClass());
        return xtream.toString();

    }

    @Override
    public String initMessage(String FromUserName, String ToUserName,ImageMessage imageMessage) {
        Image image = imageMessage.getImage();
        image.setMediaId(getmediaId());
        image.setPicUrl(image.getPicUrl());
        ImageMessage message = new ImageMessage();
        message.setFromUserName(ToUserName);
        message.setToUserName(FromUserName);
        message.setCreateTime(new Date());
        message.setImage(image);
        return messageToXml(message);
    }

    /**
     * 获取Media
     * @return
     */
    public String getmediaId(){
        String path = "f:/1.jpg";
        String accessToken = WeiXinUtil.getAccess_Token();
        String mediaId = null;
        try {
            mediaId = UploadUtil.upload(path, accessToken, "image");

        } catch (Exception e) {
            e.printStackTrace();
        }
        return mediaId;
    }

}
